﻿using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using SER = CPPEI.ETL.Domain.Services;
using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using VIW = CPPEI.ETL.Domain.Views;

namespace CPPEI.ETL.WebApi.DVPlugIn
{
    /// <summary>
    /// 获取数据审核插件程序集
    /// </summary>
    [CPPEI.WebApi.AuthGetAction("DVPLUGIN", "GetAssemblyList", "1.0")]
    public class GetAssemblyListAction : BaseAction<GetAssemblyListParams, GetAssemblyListResult>
    {
        protected override ActionResult<GetAssemblyListResult> OnExecute(ActionContext context,
            SysParameters sysParameters,
            GetAssemblyListParams opParams)
        {
            SER.DVPlugInService service = new SER.DVPlugInService();

            PagedList<ENT.EDVPlugInAssembly> serResult = service.GetAssemblyList(opParams.Filter, opParams.Page * 25, 25);

            if(serResult.IsSuccess)
            {
                GetAssemblyListResult result = new GetAssemblyListResult();

                result.Total = serResult.Total;
                result.Limit = serResult.Limit;
                result.Start = serResult.Start;
                result.Page = opParams.Page;
                foreach (var model in serResult.Data)
                {
                    GetAssemblyListResult.Item item = new GetAssemblyListResult.Item();
                    item.Id = model.Id;
                    item.Assembly = DefaultValue.GetValue(model.Assembly, string.Empty);
                    item.Version = DefaultValue.GetValue(model.Version, string.Empty);

                    result.Items.Add(item);
                }
                return new ActionResult<GetAssemblyListResult>(result);
            }
            else
            {
                return new ActionResult<GetAssemblyListResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetAssemblyListParams
    {
        public int Page { get; set; }
        public string Filter { get; set; }
    }

    public class GetAssemblyListResult : PagedResult
    {
        public List<Item> Items { get; set; }

        public GetAssemblyListResult()
        {
            Items = new List<Item>();
        }

        public class Item
        {
            public Guid Id { get; set; }

            public string Assembly { get; set; }

            public string Version { get; set; }
        }
    }
}
